<?php
	class Tahun_ajaranController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";	
		public $module = "akademik";
		public $menuId = "master_tahunajaran";
		public $loginUser;
		
		public function init() {
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");				
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_master_tahunajaran");
			$this->subMenu = "Tahun Ajaran";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"");			
			$this->render('index',$data);
		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			//$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			
			$condition = "department_id = :deptId";
			$arrParam = array(":deptId"=>$department_id);
			
			if ($onlyActive){
				$condition .= " and is_active = true";
			}
			
			//if ($tahun_ajaran_id != ""){
			//	$condition .= " and tahun_ajaran_id = :tahunAjaranId";
			//	$arrParam[":tahunAjaranId"] = $tahun_ajaran_id;
			//}
			
			$mTahunAjaran = MTahunAjaran::model()->findAll($condition . " order by id desc",$arrParam);
			
			//if ($onlyActive){
				//$mTahunAjaran = MTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and department_id = '$department_id'","order"=>"id"));
			//}else{
				//$mTahunAjaran = MTahunAjaran::model()->findAll();
				//$mTahunAjaran = MTahunAjaran::model()->findAll(array("condition"=>"department_id = '$department_id'","order"=>"id"));
			//}
						
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mTahunAjaran;
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionedit(){
			$op = Yii::app()->request->getParam("op","add");
			$startDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("start_date","01-Jan-2000")),"Y-m-d");
			$endDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("end_date","01-Jan-2000")),"Y-m-d");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_tahunajaran_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$tahunAjaran = new MTahunAjaran;
				$tahunAjaran->pos = Yii::app()->request->getParam("pos","0");
				$tahunAjaran->department_id = Yii::app()->request->getParam("department_id","");
				$tahunAjaran->name = Yii::app()->request->getParam("name","");
				$tahunAjaran->start_date = $startDate;
				$tahunAjaran->end_date = $endDate;
				$tahunAjaran->sid_prefix = Yii::app()->request->getParam("sid_prefix","");
				$tahunAjaran->description = Yii::app()->request->getParam("description","");
				$tahunAjaran->is_active=$status;
				if ($tahunAjaran->save()){
					echo "<script>
						alert('Data Sudah di tambahkan...!');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($tahunAjaran->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_master_tahunajaran_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$tahunAjaran = MTahunAjaran::model()->find("id=:id",array(":id"=>$id));
				$tahunAjaran->pos = Yii::app()->request->getParam("pos","0");
				$tahunAjaran->name = Yii::app()->request->getParam("name","");
				$tahunAjaran->start_date = $startDate;
				$tahunAjaran->end_date = $endDate;
				$tahunAjaran->sid_prefix = Yii::app()->request->getParam("sid_prefix","");
				$tahunAjaran->description = Yii::app()->request->getParam("description","");
				$tahunAjaran->is_active=$status;
				if ($tahunAjaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!."). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($tahunAjaran->getErrors());
				}
			}else if ($op == "del"){
				if (!LoginUser::getPermission("akademik_master_tahunajaran_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_tahun_ajaran where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = Yii::t("alert","Data tidak dapat di hapus !");
					$this->result["actionError"] =$e->getMessage();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
				header('Content-type:application/json');
				echo CJSON::encode($this->result);
			}
			
			
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","start_date"=>"","end_date"=>"","description"=>"","is_active"=>"1","sid_prefix"=>"");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_tahunajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
				$data["pos"] = CMTahunAjaran::getNewPos($department_id);
				
			}else if($op =="edit"){
				LoginUser::getPermission("akademik_master_tahunajaran_edit");
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
				$rsTahunAjaran = CMTahunAjaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				//print_r(CMTahunAjaran::getNextTahunAjaran($rsTahunAjaran[0]["pos"]));
				                
				
				if(count($rsTahunAjaran) > 0){
					$data["pos"] = $rsTahunAjaran[0]->pos;
					$data["department_id"] = $rsTahunAjaran[0]->department_id;
					$data["name"] = $rsTahunAjaran[0]->name;
					//$startDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("txtStartDate","")),"Y-m-d");
					$startDate = date_format(date_create_from_format( "Y-m-d", $rsTahunAjaran[0]->start_date),"d-M-Y");					
					$data["start_date"] = $startDate;
					$endDate = date_format(date_create_from_format( "Y-m-d", $rsTahunAjaran[0]->end_date),"d-M-Y");					
					$data["end_date"] = $endDate;
					$data["sid_prefix"] = $rsTahunAjaran[0]->sid_prefix;
					$data["description"] = $rsTahunAjaran[0]->description;
					$data["is_active"] = $rsTahunAjaran[0]->is_active;
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
		
		/*
		Action Created by : Michael 2014-02-26
		*/
		public function actionPrint(){ 
			$department_id= Yii::app()->request->getQuery('department_id','1');
			$this->subMenu = "Tahun Ajaran";
			$this->layout ="//layouts/ly_blank";
			//$data=array("department_id"=>"");
			$data = Yii::app()->db->createCommand()
				->select('id, name, start_date, end_date, description, is_active')
				->from('m_tahun_ajaran')
				->where('department_id=:department_id', array(':department_id'=>$department_id))
			->queryAll();		
			$this->render('print',array('data' => $data));
		}
		
		public function actionpopup(){
			$data=array("callback"=>$department_id= Yii::app()->request->getParam('callback',''));			
			$this->layout ="//layouts/ly_blank";
			$this->render("popup",$data);
		}

		
	}
?>